﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.Content;


namespace MethodWorx.CMS.Membership
{
    public class User : AggregateRoot, IAttributes
    {
        public UserType UserType { get; set; }
        public string Username{   get; set;}
        public string Password { get; set; }
        public bool Administrator { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool Active { get; set; }
        public string EmailAddress { get; set; }
        public virtual IEnumerable<Role> Roles { get; private set; }
        public virtual IEnumerable<UserDashboardWidget> DashboardWidgets { get; private set; }
        public virtual Dictionary<string, string> Attributes { get; set; }

        public User()
        {
            this.Roles = new List<Role>();
            this.DashboardWidgets = new List<UserDashboardWidget>();
        }

        public bool VerifyPassword(string password)
        {
            return this.Password == password;
        }





        public void SetDashboardWidgets(IPlugin foundPlugin, Plugins.DashboardWidget[] result)
        {
            List<UserDashboardWidget> toRemove = new List<UserDashboardWidget>();

            foreach (var dbw in this.DashboardWidgets)
            {
                if (dbw.PluginId == foundPlugin.Id)
                {
                    toRemove.Add(dbw);
                }
            }

            foreach (var dbw in toRemove)
            {
                this.RemoveDashboardWidget(dbw);
            }

            int sequence;
            if (this.DashboardWidgets.Any())
                sequence = this.DashboardWidgets.Max(dw => dw.Sequence);
            else
                sequence = 0;

            foreach(var dbw in result)
            {
                this.AddDashboardWidget(foundPlugin, dbw, sequence, 0);
            }
        }

        protected virtual void RemoveDashboardWidget(UserDashboardWidget dbw)
        {
            ((List<UserDashboardWidget>)this.DashboardWidgets).Remove(dbw);
        }

        protected virtual void AddDashboardWidget(IPlugin plugin, DashboardWidget result, int sequence, int columnIndex)
        {
            ((List<UserDashboardWidget>)this.DashboardWidgets).Add(new UserDashboardWidget(this, plugin, result, sequence, columnIndex));
        }

        public void SetAttribute(string name, string value)
        {
            if(this.Attributes == null)
                this.Attributes = new Dictionary<string, string>();

            if (this.Attributes.ContainsKey(name))
                this.Attributes[name] = value;
            else
                this.Attributes.Add(name, value);
        }

        public string GetAttribute(string name)
        {
            if (this.Attributes == null)
                this.Attributes = new Dictionary<string, string>();

            if (this.Attributes.ContainsKey(name))
                return this.Attributes[name];
            else
            {
                return null;
            }
        }
    }
}
